package com.bootdo.staticmask.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.system.controller.LoginController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.staticmask.domain.MaskRuleDO;
import com.bootdo.staticmask.service.MaskRuleService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author lkg
 * @email lkggod@163.com
 * @date 2018-04-25 11:27:35
 */
 
@Controller
@RequestMapping("/staticmask/maskRule")
public class MaskRuleController {

	private final MaskRuleService maskRuleService;

	@Autowired
	public MaskRuleController(MaskRuleService maskRuleService) {
		this.maskRuleService = maskRuleService;
	}


	@GetMapping()
	@RequiresPermissions("staticmask:maskRule:maskRule")
	String MaskRule(){
	    return "staticmask/maskRule/maskRule";
	}

	@ResponseBody
	@GetMapping("/findAll")
	public List<MaskRuleDO> findAll(){
		return maskRuleService.findAll();
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("staticmask:maskRule:maskRule")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MaskRuleDO> maskRuleList = maskRuleService.list(query);
		int total = maskRuleService.count(query);
		PageUtils pageUtils = new PageUtils(maskRuleList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("staticmask:maskRule:add")
	String add(){
	    return "staticmask/maskRule/add";
	}

	@GetMapping("/edit/{maskRuleId}")
	@RequiresPermissions("staticmask:maskRule:edit")
	String edit(@PathVariable("maskRuleId") Long maskRuleId,Model model){
		MaskRuleDO maskRule = maskRuleService.get(maskRuleId);
		model.addAttribute("maskRule", maskRule);
	    return "staticmask/maskRule/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("staticmask:maskRule:add")
	public R save( MaskRuleDO maskRule){
		maskRule.setUpdateTime(new Date());
		maskRule.setUpdateUser(LoginController.getUSER());
		if(maskRuleService.save(maskRule)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("staticmask:maskRule:edit")
	public R update( MaskRuleDO maskRule){
		maskRule.setUpdateTime(new Date());
		maskRule.setUpdateUser(LoginController.getUSER());
		maskRuleService.update(maskRule);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskRule:remove")
	public R remove( Long maskRuleId){
		if(maskRuleService.remove(maskRuleId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("staticmask:maskRule:remove")
	public R remove(@RequestParam("ids[]") Long[] maskRuleIds){
		maskRuleService.batchRemove(maskRuleIds);
		return R.ok();
	}
	
}
